﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace GenUI.WPF.GenControls
{
    public class DateTimePicker : Control
    {
        TextBox tb;
        Button btn;
        Popup popChioce;
        public DateTime DateTimeStr
        {
            get { return (DateTime)GetValue(DateTimeStrProperty); }
            set { SetValue(DateTimeStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DateTimeStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DateTimeStrProperty =
            DependencyProperty.Register("DateTimeStr", typeof(DateTime), typeof(DateTimePicker),new PropertyMetadata(DateTime.Now));

        static DateTimePicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DateTimePicker), new FrameworkPropertyMetadata(typeof(DateTimePicker)));
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            tb = GetTemplateChild("PART_tb") as TextBox;
            btn = GetTemplateChild("PART_imageBtn") as Button;
            popChioce = GetTemplateChild("PART_popChioce") as Popup;
            tb.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            btn.Click += Btn_Click;
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen == true)
            {
                popChioce.IsOpen = false;
            }

            TDateTimeView dtView = new TDateTimeView() { formerHourStr = DateTimeStr.ToString() };// TDateTimeView  构造函数传入日期时间
            dtView.DateTimeOK += (dateTimeStr) => //TDateTimeView 日期时间确定事件
            {
                DateTimeStr = DateTime.Parse(dateTimeStr);
                popChioce.IsOpen = false;//TDateTimeView 所在pop  关闭
            };

            popChioce.Child = dtView;
            popChioce.IsOpen = true;
        }
    }
}
